package LeetCode;

public class Code234_isPalindrome {

    public boolean isPalindrome(ListNode head) {
        if (head == null){
            return true;
        }
        ListNode slow = head;
        ListNode fast = head.next;
        while (fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        fast = slow.next;
        slow.next = null;
        slow = head;
        ListNode l2 = reverseList(fast);
        while (slow != null && l2 != null){
            if (slow.val != l2.val){
                return false;
            }
            slow = slow.next;
            l2 = l2.next;
        }
        return true;
    }

    private ListNode reverseList(ListNode node) {
        if (node == null || node.next == null){
            return node;
        }
        ListNode p = reverseList(node.next);
        node.next.next = node;
        node.next = null;
        return p;
    }

    public static void main(String[] args) {
        Code234_isPalindrome code234_isPalindrome = new Code234_isPalindrome();
        ListNode head = new ListNode(1);
        head.next = new ListNode(2);
        System.out.println(code234_isPalindrome.isPalindrome(head));
    }
}
